package com.zl.zlproject.ui.main;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.zl.zlproject.R;
import com.zl.zlproject.base.activity.BaseActivity;
import com.zl.zlproject.base.application.MyApplication;
import com.zl.zlproject.base.fragment.BaseFragment;
import com.zl.zlproject.model.net.api.ApiService;
import com.zl.zlproject.model.net.bean.BaseNetBean;
import com.zl.zlproject.model.net.bean.TestBean;
import com.zl.zlproject.model.net.bean.TestListBean;
import com.zl.zlproject.model.net.engine.RetrofitHelper;
import com.zl.zlproject.model.net.engine.TransformHelper;
import com.zl.zlproject.model.net.engine.intercepter.HttpResult;
import com.zl.zlproject.ui.home.fragment.HomeFragment;
import com.zl.zlproject.ui.me.fragment.MeFragment;
import com.zl.zlproject.ui.more.fragment.MoreFragment;
import com.zl.zlproject.ui.product.fragment.ProductFragment;
import com.zl.zlproject.utils.MyToast;
import com.zl.zlproject.widget.simple.SimpleOnTabSelectedListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class MainActivity extends BaseActivity {
    @BindView(R.id.layout_fragment)
    FrameLayout mLayoutFragment;
    @BindView(R.id.bottom_bar)
    BottomNavigationBar mBottomBar;


    private int mSelectedPosition = 0;

    private List<BaseFragment> mFragments = new ArrayList();

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        super.initView();
        initFragment();
        mBottomBar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottomBar
                .setActiveColor(MyApplication.appColor)//选中颜色 图标和文字
                .setInActiveColor(R.color.gray5)//默认未选择颜色
                .setBarBackgroundColor(R.color.white);//默认背景色
        mBottomBar
                .addItem(new BottomNavigationItem(R.mipmap.icon_home, "首页"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_producte, "项目"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_producte, "我的"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_more, "更多"))

                .setFirstSelectedPosition(0)//设置默认选择的按钮
                .initialise();
        onSelectedPosition(0);

    }

    private void initFragment() {
        mFragments.add(new HomeFragment());
        mFragments.add(new ProductFragment());
        mFragments.add(new MeFragment());
        mFragments.add(new MoreFragment());
    }

    @Override
    protected void initListerner() {
        super.initListerner();

        mBottomBar.setTabSelectedListener(new SimpleOnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                super.onTabSelected(position);
                onSelectedPosition(position);
            }
        });


    }




    public void onSelectedPosition(int position) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment targetFg = mFragments.get(position);
        Fragment lastFg = mFragments.get(mSelectedPosition);
        mSelectedPosition = position;
        ft.hide(lastFg);
        if (!targetFg.isAdded()) {
            ft.add(R.id.layout_fragment, targetFg);
        }
        ft.show(targetFg);
        ft.commit();
    }

    private void getListData() {
        RetrofitHelper
                .creatApi(ApiService.class)
                .getRestaurantList()
                .compose(TransformHelper.transformListOnMain(TestListBean.class))
                .subscribe(new HttpResult<BaseNetBean<List<TestListBean>>>(MainActivity.this) {
                    @Override
                    protected void onFailed() {

                    }

                    @Override
                    protected void onSuccess(BaseNetBean<List<TestListBean>> listBaseNetBean) {
                        List<TestListBean> body = listBaseNetBean.body;
                    }
                });

    }


    private void getDataSuccess() {

        RetrofitHelper
                .creatApi(ApiService.class)
                .getRestaurantDetail()
                .compose(TransformHelper.transformOnMain(TestBean.class))
                .subscribe(new HttpResult<BaseNetBean<TestBean>>(MainActivity.this) {
                    @Override
                    protected void onFailed() {
                        MyToast.show(MainActivity.this, "========error=====");
                    }

                    @Override
                    protected void onSuccess(BaseNetBean<TestBean> testBeanBaseNetBean) {
                        TestBean body = testBeanBaseNetBean.body;
                        MyToast.show(MainActivity.this, "========success=====");
                    }
                });


    }

}
